package com.flowable.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.util.CopyUtil;
import com.common.util.DateUtils;
import com.flowable.Excel.SysExpressionExcel;
import com.flowable.domain.dto.SysExpression;
import com.flowable.mapper.SysExpressionMapper;
import com.flowable.service.ISysExpressionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 流程达式Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-12
 */
@Service
public class SysExpressionServiceImpl implements ISysExpressionService {
    @Autowired
    private SysExpressionMapper sysExpressionMapper;

    /**
     * 查询流程达式
     *
     * @param id 流程达式主键
     * @return 流程达式
     */
    @Override
    public ResponseEntity selectSysExpressionById(Long id) {
        return ResponseEntity.ok(sysExpressionMapper.selectSysExpressionById(id));
    }

    /**
     * 查询流程达式列表
     *
     * @param sysExpression 流程达式
     * @return 流程达式
     */
    @Override
    public ResponseEntity selectSysExpressionList(Page pageDto, SysExpression sysExpression) {
        return ResponseEntity.ok(sysExpressionMapper.selectSysExpressionList(pageDto, sysExpression));
    }

    /**
     * 新增流程达式
     *
     * @param sysExpression 流程达式
     * @return 结果
     */
    @Override
    public int insertSysExpression(SysExpression sysExpression) {
        sysExpression.setCreateTime(DateUtils.getNowDate());
        return sysExpressionMapper.insertSysExpression(sysExpression);
    }

    @Override
    public ResponseEntity save(SysExpression sysExpression) {
        if (ObjectUtils.isEmpty(sysExpression.getId())) {
            return ResponseEntity.ok(this.insertSysExpression(sysExpression));
        } else {
            return ResponseEntity.ok(this.updateSysExpression(sysExpression));
        }
    }

    /**
     * 修改流程达式
     *
     * @param sysExpression 流程达式
     * @return 结果
     */
    @Override
    public int updateSysExpression(SysExpression sysExpression) {
        sysExpression.setUpdateTime(DateUtils.getNowDate());
        return sysExpressionMapper.updateSysExpression(sysExpression);
    }

    /**
     * 批量删除流程达式
     *
     * @param ids 需要删除的流程达式主键
     * @return 结果
     */
    @Override
    public ResponseEntity deleteSysExpressionByIds(Long[] ids) {
        return ResponseEntity.ok(sysExpressionMapper.deleteSysExpressionByIds(ids));
    }

    /**
     * 删除流程达式信息
     *
     * @param id 流程达式主键
     * @return 结果
     */
    @Override
    public int deleteSysExpressionById(Long id) {
        return sysExpressionMapper.deleteSysExpressionById(id);
    }

    @Override
    public List<SysExpressionExcel> ExportList() {
        List<SysExpression> sysExpressions = sysExpressionMapper.selectList(new LambdaQueryWrapper<SysExpression>().orderBy(true, false,SysExpression::getCreateTime));
        return CopyUtil.copyList(sysExpressions, SysExpressionExcel.class);
    }
}
